<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/29
 * Time: 13:43
 */
class Usermodel extends CI_Model{

    var $conn;

    public function __construct()
    {
        parent::__construct();
        $this->conn = $this->load->database('local', TRUE);
    }

    /**
     * 事务：处理登录
     * @param $transData
     * @return mixed
     *
     */
    public function transUserLogin($transData) {
        $this->conn->trans_start();
        $result = $this->queryUserByUnionId($transData['unionId']);
        if($result) {
            $userId = $result['id'];
            $updateData = array();
            if(!$result['openid'])
                $updateData['openId'] = $transData['openId'];
            if(stristr($result['avatarUrl'], 'https://wx.qlogo.cn/'))
                $updateData['avatarUrl'] = $transData['avatarUrl'];
            $updateData['nickName'] = $transData['nickName'];
            $updateData['refresh'] = $result['refresh'];
            $updateData['havecount'] = $result['havecount'];
            if(count($updateData))
                $this->updateUserinfo($updateData, $userId);
        }
        else {
            $transData['refresh'] = 3;
            $transData['haveCount'] = 30;
            $userId = $this->insertUserinfo($transData);
        }
        $this->conn->trans_complete();

        return $userId;
    }

    /**
     * @param $openid
     * 是否付19.8
     * @return mixed
     */
    public function isTip($openid){
        $sql = "select count(id) as num FROM qiming.qmorder WHERE flag = 1 and attach='起名神器-打赏' and openid='$openid'";
        $query = $this->conn->query($sql);
        return $query->row_array();
    }

    /**
     * 插入用户信息
     * @param $userInfo
     * @return mixed
     */
    public function insertUserInfo($userInfo) {

        $str = $this->conn->insert_string('qiming.testmember', $userInfo);
        $this->conn->query($str);
        return $this->conn->insert_id();
    }

    /**
     * 更新用户信息
     * @param $userInfo
     * @param $id
     * @return mixed
     */
    public function updateUserInfo($userInfo, $id) {
        $str = $this->conn->update_string('qiming.testmember', $userInfo, "id=".$id);
        $this->conn->query($str);
        return $this->conn->affected_rows();
    }

    /**
     * 根据unionid查找用户信息
     * @param $unionId
     * @return mixed
     */
    public function queryUserByUnionId($openId) {
        $sql = "select id,openid,nickname,avatarUrl,unionid,testresult,refresh,havecount from qiming.testmember where flag = 1 and  openid = '$openId'";
        $query = $this->conn->query($sql);
        return $query->row_array();
    }

    /**
     * 根据openid查找用户信息
     * @param $openid
     * @return mixed
     */
    public function queryUserByOpenId($openid) {
        $sql = "select nickname,avatarUrl from qiming.testmember where flag = 1 and  openid = '$openid'";
        $query = $this->conn->query($sql);
        return $query->row_array();
    }

    /**
     * 根据id获取用户信息
     * @param $id
     * @return mixed
     */
    public function queryUserById($id) {
        $sql = "select id,openid,nickname,avatarUrl,unionid,testresult,refresh,havecount from qiming.testmember where flag = 1 and  id = $id";
        $query = $this->conn->query($sql);
        return $query->row_array();
    }

    /**
     * 用户刷新日志
     * @param $refresh
     * @return mixed
     */
    public function addUserRefreshLog($lastname,$openid){
        $sql = "INSERT INTO qiming.refreshlog (lastname,openid) VALUES ('$lastname','$openid')";
        $this->conn->query($sql);
        return $this->conn->insert_id();
    }
    public function getAllArt(){
        $sql = "SELECT id,title,img,createtime,arttype FROM qiming.artList WHERE flag = 1 and arttype>0 ORDER by createtime desc";
        $query = $this->conn->query($sql);
        return $query->result_array();
    }
    /**
     * 获取最新5条文章
     * @param $artid
     * @return mixed
     */
    public function getArtList(){
        $sql = "SELECT id,title,img,createtime,arttype,author FROM qiming.artList WHERE flag = 1 ORDER by createtime desc limit 0,5";
        $query = $this->conn->query($sql);
        return $query->result_array();
    }
    /**根据id获取文章信息
     * @param $artid
     * @return mixed
     */
    public function getArtById($artid){
        $sql = "SELECT id,title,content,createtime,img,author FROM qiming.artList WHERE flag = 1 and  id = $artid";
        $query = $this->conn->query($sql);
        return $query->row_array();
    }
    public function getAllTitle(){
        $sql = "SELECT id,title FROM qiming.artList WHERE flag = 1";
        $query = $this->conn->query($sql);
        return $query->row_array();
    }
    /**
     * 添加formId
     * @param $data
     * @return mixed
     */
    public function addFormId($data) {
        $str = $this->conn->insert_string('qiming.collectform', $data);
        $this->conn->query($str);
        return $this->conn->insert_id();
    }

    /**
     * 添加浏览记录
     * @param $openid
     * @param $content
     * @param $type
     * @return mixed
     */
    public function addSeeHistory($userid,$type,$lastname,$firstname,$articid,$sex,$birthdaytime){
        $sql = "INSERT INTO qiming.seehistory(userid,type,lastname,firstname,articid,sex,birthdaytime) VALUES ('$userid',$type,'$lastname','$firstname','$articid',$sex,'$birthdaytime')";

        $this->conn->query($sql);

        return $this->conn->affected_rows();
    }

    /**
     * 获取浏览记录
     * @param $openid
     * @return mixed
     */
    public function getSeeHistory($userid,$page){
        $cur = ($page-1)*20;
        $sql = "select s.`type`,s.lastname,s.firstname,s.articid,s.sex,s.birthdaytime,a.title from qiming.seehistory s,qiming.artList a where s.userid =$userid and s.flag=1 and s.articid=a.id order by s.createtime desc limit $cur,20";
        $query = $this->conn->query($sql);
        return $query->result_array();
    }
}